import { getReqBanner,getReqHotSong } from '../../service/music'
import querySelector from '../../utils/query-selector'
// 节流函数
import throttle from '../../utils/throttle'
import { recommendStore } from '../../store/recommendSong'
import { rankingStore,rankingMap } from '../../store/rankingStore'
import { playStore } from '../../store/playState'

const throttleQuerySeletor = throttle(querySelector,100)
const app = getApp()

Page({
  data: {
    searchValue: "",
    banners: [],
    swiperImgHeight: 0,
    recommendSong: [],
    hotList: [],

    screenWidth: 375,
    recommendMenuList: [],
    rankingInfos: {},
    isRankingData: false,
    // 当前播放的歌曲
    songInfos: {},
    isPlay: false
  },
  onLoad() {
    // 请求banner数据
    this.getBanners()
    // this.fetchsongRecommend()
    this.fetchMenuListSong()
    recommendStore.onState("recommendInfo",this.handleRecommendSong)
    playStore.onStates(['currSong','isPlay'],this.handlePlaySong)

    recommendStore.dispatch('fetchRecommendSong')
    for (var key in rankingMap) {
      rankingStore.onState(key,this.handleRanking(key))
    }
    // rankingStore.onState("newRanking", this.handleNewRanking)
    // rankingStore.onState("originRanking", this.handleOriginRanking)
    // rankingStore.onState("upRanking", this.handleUpRanking)
    rankingStore.dispatch('fetchRankingData')

    // 从app.js中获取数据
    this.setData({screenWidth: app.globalData.screenWidth})
  },
  // 页面销毁时触发
  onUnload() {
    recommendStore.offState("recommendInfo",this.handleRecommendSong)

    for (var key in rankingMap) {
      rankingStore.offState(key,this.handleRanking(key))
    }
    // rankingStore.offState('newRanking',this.handleRanking('newRanking'))
    // rankingStore.offState('originRanking',this.handleRanking('originRanking'))
    // rankingStore.offState('upRanking',this.handleRanking('upRanking'))
  },
  handleRecommendSong(value) {
    if(Object.keys(value).length===0) return
    this.setData({recommendSong: value.tracks.slice(0,6)})
  },
  // 点击搜索框触发
  onClickSearch() {
    wx.navigateTo({
      url: '/pages/detail-search/detail-search'
    })
  },
  // 获取轮播图
  async getBanners() {
    const { banners } = await getReqBanner()
    this.setData({banners})
  },
  // 图片加载完成
  async onImgLoad() {
    // 动态获取图片加载完成后的高度
    const res = await throttleQuerySeletor('.swiper-img')
    this.setData({swiperImgHeight: res[0].height})
  },
  // 点击更多
  onClickMore() {
    wx.navigateTo({
      url: '/pages/detail-song/detail-song?type=recommend',
    })
  },
  fetchMenuListSong() {
    // 热门歌单
    getReqHotSong().then(res=> {
      this.setData({ hotList: res.playlists})
    })

    getReqHotSong('华语').then(res=> {
      this.setData({ recommendMenuList: res.playlists})
    })
  },
  handleRanking(ranking) {
    return value => {
      if(Object.keys(value).length===0) return
      this.setData({isRankingData: true})
      const newVal = {...this.data.rankingInfos,[ranking]: value}
      this.setData({rankingInfos: newVal})
    }
  },
  onSongTap(event) {
    const index = event.currentTarget.dataset.index
    playStore.setState('playSongs',this.data.recommendSong)
    playStore.setState('songIndex',index)
  },
  handlePlaySong({currSong,isPlay}) {
    if(currSong) {
      this.setData({songInfos: currSong})
    }
    if(isPlay !== undefined) {
      this.setData({isPlay})
    }
  },
  // 点击播放工具栏
  onPlayBlumTap() {
    wx.navigateTo({
      url: '/packagePlayer/pages/music-player/music-player',
    })
  },
  // 点击暂停 / 播放
  onPlayTap() {
    playStore.dispatch('changePlayMusicWithIdAction')
  }
  // handleNewRanking(value) {
  //   if (!value.name) return
  //   console.log(value);
  //   this.setData({ newRanking: value })
  // },
  // handleOriginRanking(value) {
  //   if (!value.name) return
  //   console.log(value);
  //   this.setData({ originRanking: value })
  // },
  // handleUpRanking(value) {
  //   if (!value.name) return
  //   console.log(value);
  //   this.setData({ upRanking: value })
  // }
})